<template>
	<view>
		<u-cell-group>
			<u-cell-item title="账号安全" @click="toAccount"></u-cell-item>
			<u-cell-item title="内存清理" :value="cacheSize" @click="onClearCache"></u-cell-item>
			<u-cell-item title="用户协议" @click="onAgreement"></u-cell-item>
			<u-cell-item title="隐私政策" @click="onPrivacy"></u-cell-item>
			<u-cell-item title="个性化推送" :arrow="false">
				<u-switch slot="right-icon" v-model="pushChecked" @change="onPushChange"></u-switch>
			</u-cell-item>
			<u-cell-item title="版本号" :value="version" :arrow="false" ></u-cell-item>
		</u-cell-group>
		
		<view class="btn-logout" @click="onLogout">退出登录</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cacheSize: '',
				pushChecked: false,
				version: '',
			}
		},
		onShow() {
			this.getCacheSize();
			this.getPushChecked();
			this.getVersion();
		},
		methods: {
			toAccount(){
				uni.navigateTo({
					url:'/pages/tab/mine/account/account'
				})
			},
			onClearCache(){
				uni.showModal({
					title: "操作确认",
					content: "确认要清理缓存吗?",
					success: (res) => {
						if (res.confirm) {
							this.clearCache()
						}
					}
				})
			},
			onAgreement(){
				getApp().globalData.webUrl = "https://www.xksndf2002.cn/agreement.html";
				uni.navigateTo({
					url: '/pages/web/web'
				})
			},
			onPrivacy(){
				getApp().globalData.webUrl = "https://www.xksndf2002.cn/privacy.html";
				uni.navigateTo({
					url: '/pages/web/web'
				})
			},
			onPushChange(event){
				uni.setStorageSync('push', event);
			},
			onLogout(){
				//应用退出到登录界面
				uni.reLaunch({
					url: '/pages/login/login',
				});
				//清空缓存：token, 登录状态
				this.clearUserCache();
				//IM退出
				uni.$TUIKit.logout();
			},
			clearUserCache(){
				uni.removeStorageSync('login');
				uni.removeStorageSync('token');
				uni.removeStorageSync('userId');
				uni.removeStorageSync('userName');
				uni.removeStorageSync('userPic');
				uni.removeStorageSync('userSex');
				uni.removeStorageSync('userTxCode');
			},
			//以下
			getCacheSize(){
				// #ifdef APP-PLUS
				let self = this;
				plus.cache.calculate(size => {
					if (size < 1024) {
						self.cacheSize = size + 'B';
					} else if (size / 1024 >= 1 && size / 1024 / 1024 < 1) {
						self.cacheSize = Math.floor((size / 1024) * 100) / 100 + 'KB';
					} else if (size / 1024 / 1024 >= 1) {
						self.cacheSize = Math.floor((size / 1024 / 1024) * 100) / 100 + 'M';
					}
				});
				// #endif
			},
			clearCache(){
				let that = this;
				// #ifdef H5
				uni.clearStorageSync();//有待修改---这样连token都清空了
				uni.showToast({
					title: '缓存清理完成',
					icon: 'none',
				});
				setTimeout(() => {
					uni.redirectTo({
						url: '/im/login/login'
					});
				}, 1000)
				// #endif
				
				// #ifdef APP-PLUS
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  	
									that.getAppInfo(); // 重新计算缓存
								}, function(e) {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							console.log('文件路径读取失败')
						});
					}
					uni.showToast({
						title: '缓存清理完成',
						duration: 2000
					});
					this.cacheSize = '0KB'
				} else { // ios  
					plus.cache.clear(function() {
						uni.showToast({
							title: '缓存清理完成',
							duration: 2000
						});
						this.cacheSize = '0KB'
					});
					
				}
				// #endif
			},
			getPushChecked(){
				let checked = uni.getStorageSync('push');
				if(checked){
					this.pushChecked = checked;
				}
			},
			getVersion(){
				// #ifdef APP-PLUS
				plus.runtime.getProperty(plus.runtime.appid, (info) => {
					this.version = 'v'+info.version;
				});
				// #endif
			}
		}
	}
</script>

<style lang="scss" scoped>
	
	.btn-logout{
		margin: 100rpx 50rpx;
		
		box-sizing: border-box;
		background-image: linear-gradient(to right, #9fe9ff, #ab7aff);
		border-radius: 40rpx;
		width: 100wh;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		color:white;
	}

</style>
